package org.example.project.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

/**
 * 班级选择器组件
 */
@Composable
fun ClassSelector(
    selectedClass: String,
    onClassSelected: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    val predefinedClasses = listOf(
        "小一班",
        "小二班",
        "中一班",
        "中二班",
        "大一班",
        "大二班",
        "大三班"
    )
    
    Column(modifier = modifier) {
        Text(
            "班级",
            style = MaterialTheme.typography.caption,
            color = MaterialTheme.colors.primary
        )
        
        Spacer(modifier = Modifier.height(4.dp))
        
        // 班级快速选择
        LazyRow(
            horizontalArrangement = Arrangement.spacedBy(8.dp),
            modifier = Modifier.fillMaxWidth()
        ) {
            items(predefinedClasses) { className ->
                val isSelected = className == selectedClass
                
                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(4.dp))
                        .background(
                            if (isSelected) MaterialTheme.colors.primary
                            else MaterialTheme.colors.surface
                        )
                        .border(
                            width = 1.dp,
                            color = if (isSelected) MaterialTheme.colors.primary
                                   else MaterialTheme.colors.onSurface.copy(alpha = 0.2f),
                            shape = RoundedCornerShape(4.dp)
                        )
                        .clickable { onClassSelected(className) }
                        .padding(horizontal = 12.dp, vertical = 8.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = className,
                        color = if (isSelected) MaterialTheme.colors.onPrimary
                               else MaterialTheme.colors.onSurface,
                        fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal
                    )
                }
            }
        }
        
        Spacer(modifier = Modifier.height(8.dp))
        
        // 班级输入框，允许自定义班级
        OutlinedTextField(
            value = selectedClass,
            onValueChange = { onClassSelected(it) },
            label = { Text("班级名称") },
            modifier = Modifier.fillMaxWidth(),
            colors = TextFieldDefaults.outlinedTextFieldColors(
                focusedBorderColor = MaterialTheme.colors.primary,
                unfocusedBorderColor = MaterialTheme.colors.primary.copy(alpha = 0.5f),
                cursorColor = MaterialTheme.colors.primary
            )
        )
    }
}